<%@ page import="entity.User" %>
<%@ page import="java.util.*" %><%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2025/9/9
  Time: 15:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL表达式</title>
</head>
<body>
<%
//模拟Java代码
  pageContext.setAttribute("msg","Hello pageContext");
  request.setAttribute("mag","Hello request");
  session.setAttribute("msg","Hello session");
  application.setAttribute("mag","Hello application");
  request.setAttribute("name","admin");


  User user=new User("admin","123456");
  request.setAttribute("name","admin");

  User user2=new User("jack","123457");
  User user3=new User("tom","123465");
  User user4=new User("luck","123786");
  List<User> list = new ArrayList<>(Arrays.asList(user, user2, user3, user4));
  request.setAttribute("list", list);

  Map<String, User> map = new HashMap<>();
  map.put("u1", user);
  map.put("u2", user2);
  map.put("u3", user3);
  map.put("u4", user4);
  request.setAttribute("map", map);
%>
<h2>获取作用域</h2>
<%--
可以通过作用于对象，key的方式获取指定key的数据
可以直接通过key表示获取作用于的数据
直接使用key的情况会自动搜索所有作用域中的所欲数据
其检索索引由小到大，只在其中每一个作用域中找到了数据，则不继续查找
pageContext->request->session->application

--%>
pageContext:${pageScope.msg}<br>
request:${requestScope.msg}<br>
session:${sessionScope.msg}<br>
application:${applicationScope.msg}<br>
name:${name}<br>
msg:${msg}<br>
<%--如果四大作用域中没有数据则什么都不显示--%>
password:${password}

<h2>获取应用名</h2>
应用名:<%=request.getContextPath()%><br>
应用名:<%
  HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
  String path = req.getContextPath();
  out.print(path);
%><br>
<!--
    在EL表达式中,如果遇到对象.getXxx的语法时,可以直接通过对象.xxx表示
-->
应用名:${pageContext.request.contextPath}
<h2>获取对象</h2>
user:${user}<br>
用户名:${user.username}<br>
密码:${user.password}<br>
<h2>访问List集合</h2>
${list[0]}<br>
${list[1]}<br>
${list[2]}<br>
${list[3].username}<br>
<h2>访问Map集合</h2>
${map['u1']}<br>
${map.u2}<br>
${map.u3.username}<br>
${map['u4'].password}<br>
<h2>运算</h2>
${3+2}<br>
${3-2}<br>
${3*2}<br>
${3/2}<br>
${3%2}<br>
${3 == 2}<br>
${3 == "3"}<br>
${3 > 2 ? "3大于2" :"小于"}<br>
${3 > 2 and 2 > 3}<br>
${3 > 2 && 2 > 3}<br>
${3 > 2 or 2 > 3}<br>
${3 > 2 || 2 > 3}<br>
${empty user}<br>
${empty emp}<br>
${user == null}<br>
${emp == null}<br>
</body>
</html>
